package org.wu.framework.authorization.platform.infrastructure.persistence;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.wu.framework.authorization.platform.domain.model.sys.zone.application.SysZoneApplication;
import org.wu.framework.authorization.platform.domain.model.sys.zone.application.SysZoneApplicationRepository;
import org.wu.framework.authorization.platform.infrastructure.converter.SysZoneApplicationConverter;
import org.wu.framework.authorization.platform.infrastructure.entity.SysZoneApplicationDO;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 应用信息
 *
 * @author Jia wei Wu
 * @date 2024/04/02 06:21 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysZoneApplicationRepositoryImpl implements SysZoneApplicationRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增应用信息
     *
     * @param sysZoneApplication 新增应用信息
     * @return {@link Result< SysZoneApplication >} 应用信息新增后领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<SysZoneApplication> story(SysZoneApplication sysZoneApplication) {
        SysZoneApplicationDO sysZoneApplicationDO = SysZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(sysZoneApplication);
        lazyLambdaStream.upsert(sysZoneApplicationDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增应用信息
     *
     * @param sysZoneApplicationList 批量新增应用信息
     * @return {@link Result<List< SysZoneApplication >>} 应用信息新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<List<SysZoneApplication>> batchStory(List<SysZoneApplication> sysZoneApplicationList) {
        List<SysZoneApplicationDO> sysZoneApplicationDOList = sysZoneApplicationList.stream().map(SysZoneApplicationConverter.INSTANCE::fromLazyZoneApplication).collect(Collectors.toList());
        lazyLambdaStream.upsert(sysZoneApplicationDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个应用信息
     *
     * @param sysZoneApplication 查询单个应用信息
     * @return {@link Result< SysZoneApplication >} 应用信息领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<SysZoneApplication> findOne(SysZoneApplication sysZoneApplication) {
        SysZoneApplicationDO sysZoneApplicationDO = SysZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(sysZoneApplication);
        SysZoneApplication sysZoneApplicationOne = lazyLambdaStream
                .selectOne(
                        LazyWrappers.lambdaWrapperBean(sysZoneApplicationDO).eq(SysZoneApplicationDO::getIsDeleted, false)
                        , SysZoneApplication.class);
        return ResultFactory.successOf(sysZoneApplicationOne);
    }

    /**
     * describe 查询多个应用信息
     *
     * @param sysZoneApplication 查询多个应用信息
     * @return {@link Result<List< SysZoneApplication >>} 应用信息领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<List<SysZoneApplication>> findList(SysZoneApplication sysZoneApplication) {
        SysZoneApplicationDO sysZoneApplicationDO = SysZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(sysZoneApplication);
        List<SysZoneApplication> sysZoneApplicationList = lazyLambdaStream
                .selectList(
                        LazyWrappers.lambdaWrapperBean(sysZoneApplicationDO)
                                .eq(SysZoneApplicationDO::getIsDeleted, false)
                        , SysZoneApplication.class);
        return ResultFactory.successOf(sysZoneApplicationList);
    }

    /**
     * describe 分页查询多个应用信息
     *
     * @param size                当前页数
     * @param current             当前页
     * @param sysZoneApplication 分页查询多个应用信息
     * @return {@link Result<LazyPage< SysZoneApplication >>} 分页应用信息领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<LazyPage<SysZoneApplication>> findPage(int size, int current, SysZoneApplication sysZoneApplication) {
        SysZoneApplicationDO sysZoneApplicationDO = SysZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(sysZoneApplication);
        LazyPage<SysZoneApplication> lazyPage = new LazyPage<>(current, size);
        LazyPage<SysZoneApplication> lazyZoneApplicationLazyPage = lazyLambdaStream
                .selectPage(
                        LazyWrappers.lambdaWrapperBean(sysZoneApplicationDO)
                                .eq(SysZoneApplicationDO::getIsDeleted, false),
                        lazyPage,
                        SysZoneApplication.class);
        return ResultFactory.successOf(lazyZoneApplicationLazyPage);
    }

    /**
     * describe 删除应用信息
     *
     * @param sysZoneApplication 删除应用信息
     * @return {@link Result< SysZoneApplication >} 应用信息
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<SysZoneApplication> remove(SysZoneApplication sysZoneApplication) {
        SysZoneApplicationDO updateEntity = new SysZoneApplicationDO();
        updateEntity.setIsDeleted(true);
        lazyLambdaStream.update(updateEntity, LazyWrappers.<SysZoneApplicationDO>lambdaWrapper()
                .eq(SysZoneApplicationDO::getId, sysZoneApplication.getId()));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在应用信息
     *
     * @param sysZoneApplication 应用信息领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<Boolean> exists(SysZoneApplication sysZoneApplication) {
        SysZoneApplicationDO sysZoneApplicationDO = SysZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(sysZoneApplication);
        Boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(sysZoneApplicationDO));
        return ResultFactory.successOf(exists);
    }

}